package dev.ece.util.tools.transformations;

import com.bulletphysics.collision.dispatch.CollisionObject;
import com.bulletphysics.linearmath.Transform;

import dev.ece.util.lib.Matrix4f;
import dev.ece.util.lib.Vector3f;

public class JbulletTransformation implements Transformation {

	private final static Transform transform = new Transform();
	
	private CollisionObject collisionObject;
	
	private Vector3f scale;
	
	private boolean exist;
	
	private boolean existScale;
	
	public JbulletTransformation() {}
	
	public JbulletTransformation(CollisionObject collisionObject) {
		this.setCollisionObject(collisionObject);
	}
	
	public JbulletTransformation(CollisionObject collisionObject, Vector3f scale) {
		this.setCollisionObject(collisionObject);
		this.setScale(scale);
	}
	
	@Override
	public void transform(Matrix4f matrix) {
		if(isExist()) {
			collisionObject.getWorldTransform(transform);
			matrix.from(transform);
		}
		if(isExistScale()) {
			matrix.scale(scale);
		}
	}

	public void removeCollisionObject() {
		this.setScale(null);
		this.setCollisionObject(null);
	}
	
	public CollisionObject getCollisionObject() {
		return collisionObject;
	}
	
	public void setCollisionObject(CollisionObject collisionObject) {
		this.collisionObject = collisionObject;
		this.exist = this.collisionObject != null;
	}

	public boolean isExist() {
		return exist;
	}

	public boolean isExistScale() {
		return existScale;
	}

	public Vector3f getScale() {
		return scale;
	}

	public void setScale(Vector3f scale) {
		this.scale = scale;
		this.existScale = scale != null;
	}
	
}
